//
//  TopicsViewController.m
//  Alltelevisionnews
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "TopicsViewController.h"

@interface TopicsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *myTabelView;
@property(nonatomic, strong)NSString *strURL;
//@property(nonatomic, strong)UISearchBar *search;
@property(nonatomic, strong)NSMutableArray *arr;
@property(nonatomic, strong)MBProgressHUD *hud;
@property(nonatomic, assign)NSInteger page;
@end

@implementation TopicsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
//    self.title = @"标题";
    self.navigationController.navigationBar.translucent = NO;
    
    UIButton *menuBtu = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtu.frame = CGRectMake(0, 0, 20, 18);
    [menuBtu setBackgroundImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
    [menuBtu addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtu];
    

    ///////tableview适应
    self.myTabelView = [[UITableView alloc] init];
    [self.view addSubview:self.myTabelView];
    [self.myTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.height.equalTo(self.view);

    }];
    self.myTabelView.rowHeight = 100;
    
    
    self.myTabelView.dataSource = self;
    self.myTabelView.delegate = self;
    
    /// 加载菊花
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"正在加载";
    
    if ([ZDQSingleton shareSingleton].isLongin == YES) {
        self.myTabelView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
    } else {
        self.myTabelView.backgroundColor = [UIColor whiteColor];
    }
    
    self.arr =[NSMutableArray array];
    [self hearder];
    [self footer];
}



#pragma mark button的点击方法
- (void)openOrCloseLeftList {
    
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (tempAppDelegate.leftslideVC.closed) {
        
        [tempAppDelegate.leftslideVC openLeftView];
        
    } else {
        
        [tempAppDelegate.leftslideVC closeLeftView];
        
    }
    
}


#pragma mark 上拉加载下拉刷新

- (void)hearder {
    
    self.myTabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        self.strURL = [NSString stringWithFormat:@"http://api.iclient.ifeng.com/ClientNews?id=ZTPD,FOCUSZTPD&page=%ld", self.page];
        
        [self createData];
        [self.myTabelView.mj_header endRefreshing];
        
    }];
    [self.myTabelView.mj_header beginRefreshing];
    
}



- (void)footer {
    
    self.myTabelView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       
        self.page++;
        self.strURL = [NSString stringWithFormat:@"http://api.iclient.ifeng.com/ClientNews?id=ZTPD,FOCUSZTPD&page=%ld", self.page];
        
        [self createData];

        [self.myTabelView.mj_footer endRefreshing];
        
    }];
    
}



- (void)createData {
    
    [SLAFNetWorkTool cacheGetWithURL:self.strURL cookie:nil success:^(id responseObject) {
        
        if ([(NSArray *)responseObject count] != 0) {
            NSArray *tempArr = [TopicsModel baseModelByArr:responseObject[0][@"item"]];
            [self.arr addObjectsFromArray:tempArr];
        }
        
        [self.myTabelView reloadData];
        [self.hud removeFromSuperview];
        
    } failure:^(NSError *error) {
        // 失败走的方法
    }];
}

- (void)footerAction {
    self.myTabelView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSString *url = [self.strURL stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld", (long)self.page++]];
       [SLAFNetWorkTool cacheGetWithURL:url cookie:nil success:^(id responseObject) {
           if ([(NSArray *)responseObject count] != 0) {
               NSArray *tempArr = [TopicsModel baseModelByArr:responseObject[0][@"item"]];
               [self.arr addObjectsFromArray:tempArr];
           }
           [self.myTabelView.mj_footer endRefreshing];
           [self.myTabelView reloadData];

       } failure:^(NSError *error) {
           
       }];
 }];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuse = @"reuse";
    TopicPageCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[TopicPageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    TopicsModel *topics = self.arr[indexPath.row];
    cell.titleLabel.text = topics.title;
    [cell.picImage sd_setImageWithURL:[NSURL URLWithString:topics.thumbnail] placeholderImage:[UIImage imageNamed:@"pac.png"]];;
    cell.talkLabel.text = [NSString stringWithFormat:@"%@ 阅", topics.commentsall];
    cell.topicLabel.text = @"专题";
    cell.topicLabel.textColor = [UIColor redColor];
    
    if ([ZDQSingleton shareSingleton].isLongin == YES) {
        cell.titleLabel.textColor = [UIColor whiteColor];
        cell.picImage.alpha = 0.4;
        cell.talkLabel.textColor = [UIColor whiteColor];
    } else {
        
        cell.titleLabel.textColor = [UIColor blackColor];
        cell.picImage.alpha = 1;
        cell.talkLabel.textColor = [UIColor blackColor];
    }
    
    return cell;
}

#pragma mark tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TopicsModel *top = self.arr[indexPath.row];
    DetaisViewController *detaisVC = [[DetaisViewController alloc] init];
//    [self.navigationController pushViewController:detaisVC animated:YES];
     detaisVC.strId = top.topId;
    [self.navigationController pushViewController:detaisVC animated:YES];
}


//- (void)viewWillAppear:(BOOL)animated {
//    self.navigationController.navigationBar.hidden = YES;
//}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
